package web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import model.Employee;
import model.Enterprise;
import model.Person;
import model.SubjectAttribute;
import model.UserAccount;
import model.type.SubjectAttributeType;

public class Html {
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMM dd, yyyy", Locale.ENGLISH);
	
	public static String personLink(Person p){
		try{
			if(p == null) return "Nobody";
			else return "<a class=\"person\" href=\"person?edit="+p.getId()+"\">"+p+"</a>";
		} catch(NullPointerException err){
			return "Database error";
		}
	}
	
	public static String userLink(UserAccount u){
		try{
			if(u == null) return "none";
			else return "<a class=\"user\" href=\"user?edit="+u.getId()+"\">"+u.getUsername()+"</a>";
		} catch(NullPointerException err){
			return "Database error";
		}
	}
	
	public static String enterpriseLink(Enterprise e){
		try{
			if(e == null) return "none";
			else return "<a class=\"enterprise\" href=\"enterprise?edit="+e.getId()+"\">"+e.getName()+"</a>";
		} catch(NullPointerException err){
			return "Database error";
		}
	}
	
	public static String personLink(Employee e){
		if(e == null) return "Nobody";
		return personLink(e.getPersonObject());
	}
	
	public static String date(Date d){	
		if(d == null) return "Undefined";
		else return dateFormat.format(d);		
	}
	
	public static String searchAttributes(ArrayList<SubjectAttributeType> list, String className){
		StringBuilder strb = new StringBuilder();
		for(SubjectAttributeType s : list){
			strb.append("<tr class=\""+className+"\"><td>"+s.getName()+"</td><td><input id=\""+s.getId()+"\" name = \""+s.getId()+"\"></input></td></tr>\n");
		}
		return strb.toString();
	}
	
	public static String notNull(Object o){
		try{
			return o.toString();
		} catch(Exception e){
			return "";
		}
	}
}
